using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    public class NewsletterSubscriber
    {
        private int _subscriberId;
        public int NewsletterSubscriberId
        {
            get { return this._subscriberId; }
            set { this._subscriberId = value; }
        }

        private string _subscriberName;
        public string SubscriberName
        {
            get { return this._subscriberName; }
            set { this._subscriberName = value; }
        }

        private string _email;
        public string Email
        {
            get { return this._email; }
            set { this._email = value; }
        }

        private int _groupId;
        public int NewsletterGroupId
        {
            get { return this._groupId; }
            set { this._groupId = value; }
        }

        private NewsletterGroup _group;
        public NewsletterGroup Group
        {
            get
            {
                if(_group == null)
                    _group = NewsletterGroup.Load(NewsletterGroupId);
                return _group;
            }
            set
            {
                NewsletterGroupId = value.GroupId;
                _group = value;
            }
        }

        public string NewsletterGroupName
        {
            get { return Group.GroupName; }
            set { Group.GroupName = value; }
        }

        public NewsletterSubscriber()
        {
            this._subscriberId = 0;
            this._subscriberName = null;
            this._email = null;
            this._groupId = 0;
        }

        public NewsletterSubscriber(int subscriberId, string subscriberName, string email, int groupId)
        {
            this._subscriberId = subscriberId;
            this._subscriberName = subscriberName;
            this._email = email;
            this._groupId = groupId;
        }

        public NewsletterSubscriber(TblNewsletterSubscriber subscriber)
        {
            this._subscriberId = subscriber.NewsletterSubscriberId;
            this._subscriberName = subscriber.SubscriberName;
            this._email = subscriber.Email;
            this._groupId = subscriber.NewsletterGroupId;
        }

        public static NewsletterSubscriber Load(int subscriberId)
        {
            TblNewsletterSubscriber subscriber = TblNewsletterSubscriber.GetTblNewsletterSubscriberByNewsletterSubscriberId(subscriberId);
            return new NewsletterSubscriber(subscriber);
        }

        public static void DeleteSubscribers(List<int> ids)
        {
            TblNewsletterSubscriber.DeleteTblNewsletterSubscriberList(ids);
        }
        
        public static void DeleteSubscribers(ReadWriteCustomCollection<NewsletterSubscriber> subscribers)
        {
            List<int> ids = new List<int>();
            foreach (NewsletterSubscriber subscriber in subscribers)
            {
                ids.Add(subscriber.NewsletterSubscriberId);
            }
            TblNewsletterSubscriber.DeleteTblNewsletterSubscriberList(ids);
        }

        public void Update()
        {
            TblNewsletterSubscriber subscriber = new TblNewsletterSubscriber(NewsletterSubscriberId,
                SubscriberName, Email, NewsletterGroupId);
            subscriber.Save();
            NewsletterSubscriberId = subscriber.NewsletterSubscriberId;
        }

        private static ReadWriteCustomCollection<NewsletterSubscriber> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblNewsletterSubscriber> data)
        {
            ReadWriteCustomCollection<NewsletterSubscriber> result = new ReadWriteCustomCollection<NewsletterSubscriber>();

            for (int i = 0; i < data.Count; i++)
            {
                result.Add(new NewsletterSubscriber(data[i]));
            }

            return result;
        }
        public static ReadWriteCustomCollection<NewsletterSubscriber> GetSubscribtersByGroup(int SubscriberGroupId)
        {
            ReadWriteCustomCollection<NewsletterSubscriber> groups = GetPagedSubscribtersByGroup(1, 99999, "", SubscriberGroupId);
            return groups;
        }

        public static ReadWriteCustomCollection<NewsletterSubscriber> GetPagedSubscribtersByGroup(int CurrentPageIndex, int PageSize, string SortBy, int SubscriberGroupId)
        {
            int startRow = (CurrentPageIndex - 1) * PageSize;
            ECommerce.DAL.CustomCollection<TblNewsletterSubscriber> data = TblNewsletterSubscriberExtended.GetAllTblNewsletterSubscriberByGroup(SortBy, startRow, PageSize, SubscriberGroupId);
            ReadWriteCustomCollection<NewsletterSubscriber> groups = LoadFromCustomCollection(data);
            return groups;
        }

        public static ReadWriteCustomCollection<NewsletterSubscriber> GetPagedSubscribters(int CurrentPageIndex, int PageSize, string SortBy)
        {
            int startRow = (CurrentPageIndex - 1) *PageSize;
            ECommerce.DAL.CustomCollection<TblNewsletterSubscriber> data = TblNewsletterSubscriber.GetAllTblNewsletterSubscriberPaged(SortBy, startRow, PageSize);
            ReadWriteCustomCollection<NewsletterSubscriber> groups = LoadFromCustomCollection(data);
            return groups;
        }

        public static ReadWriteCustomCollection<NewsletterSubscriber> GetPagedSubscribers(int CurrentPageIndex, int PageSize, string SortBy)
        {
            ECommerce.DAL.CustomCollection<TblNewsletterSubscriber> data = TblNewsletterSubscriber.GetAllTblNewsletterSubscriberPaged(SortBy, CurrentPageIndex, PageSize);
            ReadWriteCustomCollection<NewsletterSubscriber> groups = LoadFromCustomCollection(data);
            return groups;
        }
        public static int GetSubscriberCount()
        {
            return TblNewsletterSubscriber.GetAllTblNewsletterSubscriberPagedCount("", 0, 0);
        }

        public static int GetSubscriberCountByGroup(int SubscriberGroupId)
        {
            return TblNewsletterSubscriberExtended.GetAllTblNewsletterSubscriberCountByGroup(SubscriberGroupId);
        }   
    }
}
